package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateWalletMoneyConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateWalletMoneyConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWalletMoneyConfigLogic {
	return &UpdateWalletMoneyConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改钱包币种配置
func (l *UpdateWalletMoneyConfigLogic) UpdateWalletMoneyConfig(in *pb.UpdateWalletMoneyConfigReq) (*pb.UpdateWalletMoneyConfigResp, error) {
	defer utils.RecoverPanic()

	var wmc *wallet.WalletMoneyConfig
	var err error
	wmc, err = l.svcCtx.WalletMoneyConfigModel.FindOne(l.ctx, in.MoneyType)
	if err != nil && err != wallet.ErrNotFound {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	if wmc == nil {
		l.Logger.Errorf("WalletMoneyConfig not found ")
		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "not found")
	}

	if in.State != 0 {
		wmc.State = in.State
	}
	if in.MoneyName != "" {
		wmc.MoneyName = in.MoneyName
	}
	if in.MoneyIcon != "" {
		wmc.MoneyIcon = in.MoneyIcon
	}
	//if in.MinWithdrawAmount != 0 {
	//	wmc.MinWithdrawAmount = in.MinWithdrawAmount
	//}
	//if in.MaxWithdrawAmount != 0 {
	//	wmc.MaxWithdrawAmount = in.MaxWithdrawAmount
	//}
	//if in.MinDepositAmount != 0 {
	//	wmc.MinDepositAmount = in.MinDepositAmount
	//}
	//if in.MaxDepositAmount != 0 {
	//	wmc.MaxDepositAmount = in.MaxDepositAmount
	//}

	err = l.svcCtx.WalletMoneyConfigModel.Update(l.ctx, wmc)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	return &pb.UpdateWalletMoneyConfigResp{}, nil
}
